package com.yiyuan.app.config;

import com.guaike.core.web.helper.RequestSignHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;

/**
 * @Author: zhangxilin
 * @Date: 2024/9/25
 */
@Component
public class ImportBizSignConfg {

    private final static String DATA_ID = "biz.sign.conf";

    private final static String LINE_SEP = "\n";


    @PostConstruct
    public void init() throws IOException {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream(DATA_ID);
        String content = getFileContent(in);
        if (StringUtils.isNotBlank(content)) {
            // 获取缓存里的配置信息并处理
            RequestSignHelper.handle(content);
        }
    }


    private String getFileContent(Object fileInPath) throws IOException {
        BufferedReader br = null;
        if (fileInPath == null) {
            return null;
        }
        if (fileInPath instanceof String) {
            br = new BufferedReader(new FileReader(new File((String) fileInPath)));
        } else if (fileInPath instanceof InputStream) {
            br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
        }
        if (br == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line).append(LINE_SEP);
        }
        br.close();
        return sb.toString();
    }
}
